package org.lk.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.lk.entity.CdcJob;
import org.lk.entity.CdcJobTable;
import org.lk.param.query.CdcJobQuery;
import org.lk.param.saveorupdate.CdcJobParam;
import org.lk.service.CdcJobService;
import org.lk.service.CdcJobTableService;
import org.lk.util.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: lk
 * @since:
 */
@RestController
@RequestMapping("cdcJob")
public class CdcJobController {
    @Autowired
    private CdcJobService cdcJobService;

    @Autowired
    private CdcJobTableService cdcJobTableService;


    @PostMapping("page")
    public ResponseResult page(@RequestBody CdcJobQuery cdcJobQuery) {
        Page<CdcJob> page = Page.of(cdcJobQuery.getCurrent(), cdcJobQuery.getSize());
        QueryWrapper<CdcJob> query = Wrappers.query();
        query.eq(StrUtil.isNotEmpty(cdcJobQuery.getJobName()), "job_name", cdcJobQuery.getJobName());
        Page<CdcJob> pageData = cdcJobService.page(page, query);
        return ResponseResult.success(pageData);
    }


    @PostMapping("saveOrUpdate")
    public ResponseResult saveOrUpdate(@RequestBody CdcJobParam cdcJobParam) {
        // 校验任务名称是否重复
        QueryWrapper<CdcJob> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("job_name",cdcJobParam.getJobName());
        CdcJob one = cdcJobService.getOne(queryWrapper1);
        if (one!=null){
            return ResponseResult.fail("名称重复,请重新输入");
        }
        CdcJob cdcJob = new CdcJob();
        BeanUtils.copyProperties(cdcJobParam, cdcJob);
        cdcJobService.saveOrUpdate(cdcJob);
        // 保存要同步的表
        List<String> tables = cdcJobParam.getTables();
        if (CollUtil.isNotEmpty(tables)) {
            tables.forEach(table -> {
                QueryWrapper<CdcJobTable> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("cdc_id", cdcJob.getId());
                queryWrapper.eq("table_name", table);
                CdcJobTable cdcJobTable = cdcJobTableService.getOne(queryWrapper);
                if (cdcJobTable == null) {
                    cdcJobTable = new CdcJobTable();
                    cdcJobTable.setCdcId(cdcJob.getId());
                }
                cdcJobTable.setTableName(table);
                cdcJobTableService.saveOrUpdate(cdcJobTable);
            });
        }

        return ResponseResult.success();
    }

    @DeleteMapping("{id}")
    public ResponseResult delete(@PathVariable("id") String id) {
        cdcJobService.removeById(Long.parseLong(id));
        return ResponseResult.success();
    }
}
